package org.codegallery.javagal.concurrent.cpj;

import java.awt.Dimension;

public class BasicBoxSource extends SingleOutputPushStage implements
		PushSource, Runnable {

	protected final Dimension size; // maximum sizes
	protected final int productionTime; // simulated delay

	public BasicBoxSource(Dimension s, int delay) {
		size = s;
		productionTime = delay;
	}

	protected Box makeBox() {
		return new BasicBox((int) (Math.random() * size.width) + 1,
				(int) (Math.random() * size.height) + 1);
	}

	public void produce() {
		next1().putA(makeBox());
	}

	public void run() {
		try {
			for (;;) {
				produce();
				Thread.sleep((int) (Math.random() * 2 * productionTime));
			}
		} catch (InterruptedException ie) {
		} // die
	}

}
